# -*- coding:utf-8 -*-
import urlparse,re,urllib2,MySQLdb,datetime,time
import jiangpaibang


data='''
空调 http://www.etao.com/pk/index.php?cat=350401
LCD液晶电视 http://www.etao.com/pk/index.php?cat=50008224
LED电视 http://www.etao.com/pk/index.php?cat=50019790
3D电视 http://www.etao.com/pk/index.php?cat=50020200
冰箱 http://www.etao.com/pk/index.php?cat=50003881
等离子电视 http://www.etao.com/pk/index.php?cat=50016745
洗衣机 http://www.etao.com/pk/index.php?cat=350301
剃须刀 http://www.etao.com/pk/index.php?cat=350201
电吹风 http://www.etao.com/pk/index.php?cat=350213
空气净化器 http://www.etao.com/pk/index.php?cat=350402
吸尘器 http://www.etao.com/pk/index.php?cat=50008554
电风扇 http://www.etao.com/pk/index.php?cat=50008557
除湿机 http://www.etao.com/pk/index.php?cat=50017072
电磁炉 http://www.etao.com/pk/index.php?cat=350502
消毒柜 http://www.etao.com/pk/index.php?cat=350503
油烟机 http://www.etao.com/pk/index.php?cat=350511
微波炉 http://www.etao.com/pk/index.php?cat=50002809
豆浆机 http://www.etao.com/pk/index.php?cat=50008556
燃气灶 http://www.etao.com/pk/index.php?cat=50015382
烟灶消套装 http://www.etao.com/pk/index.php?cat=50018263
DV http://www.etao.com/pk/index.php?cat=1402
手机 http://www.etao.com/pk/index.php?cat=1512
单反 http://www.etao.com/pk/index.php?cat=50003773
平板电脑 http://www.etao.com/pk/index.php?cat=50019780
单电微单 http://www.etao.com/pk/index.php?cat=50021422
'''
#data='DC http://www.etao.com/pk/pk/index.php?cat=1403'
data=data.decode('utf-8')

def get_data(patt,data):
    m=re.search(patt,data)
    return m
def start():
    cxn=MySQLdb.connect(db='duibi',host='localhost',user='root',passwd='123456ms')
    cxn.set_character_set('utf8')
    cur=cxn.cursor()
    item_list=data.split('\n')
    dt=datetime.datetime.now()
    time_format='%Y-%m-%d %H:%M:%S'
    update_time=dt.strftime(time_format)
    print update_time
    for item in item_list:
        if item:
            litem=item.split(' ')
            type1=litem[0].strip()
            main_url=litem[1].strip()
            print type1,main_url
            res=urllib2.urlopen(main_url)
            content=res.read()
            time.sleep(1)
            content=content.decode('gb2312')
            #print content
            patt='<tr>[\s\S]+?</tr>'
            tr_list=re.findall(patt,content)
            for tr in tr_list:
                jin,yin,tong='','',''
                n_p='<td class="item-name" title="([\s\S]+?)">'
                m=re.search(n_p,tr)
                if m:
                    name=m.group(1)
                url_p='<a href="([\s\S]+?)" target="_blank">'
                m=re.search(url_p,tr)
                if m:
                    url=m.group(1)
                type1=type1

                pai_p='<td class="first">[\s\S]+?</td>'
                pai_list=re.findall(pai_p,tr)
                for pai in pai_list:
                    patt='title="[\s\S]+?">([\s\S]+?)</a>[\s\S]+?</span>([\s\S]+?)</div>'
                    m=re.search(patt,pai)
                    if m:
                        jin=m.group(1)+':'+m.group(2)
                pai_p='<td class="second">[\s\S]+?</td>'
                pai_list=re.findall(pai_p,tr)
                for pai in pai_list:
                    patt='title="[\s\S]+?">([\s\S]+?)</a>[\s\S]+?</span>([\s\S]+?)</div>'
                    m=re.search(patt,pai)
                    if m:
                        yin=m.group(1)+':'+m.group(2)

                pai_p='<td class="third">[\s\S]+?</td>'
                pai_list=re.findall(pai_p,tr)
                for pai in pai_list:
                    patt='title="[\s\S]+?">([\s\S]+?)</a>[\s\S]+?</span>([\s\S]+?)</div>'
                    m=re.search(patt,pai)
                    if m:
                        tong=m.group(1)+':'+m.group(2)
                if jin and yin and tong:
                    sql='insert into yitao (name,url,type,jin,yin,tong,update_time) values(%s,%s,%s,%s,%s,%s,%s)'
                    cur.execute(sql,(name,url,type1,jin,yin,tong,update_time))

    jiangpaibang.main(update_time)
    cxn.commit()
    cur.close()
    cxn.close()

def main():
    while True:
        try:
            start()
            time.sleep(60*30)
        except:
            time.sleep(60*30)
    
main()








